package com.face.face.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.face.face.R;
import com.poya.bankcertificationsdk.model.constant.ExternalConstant;
import com.poya.bankcertificationsdk.view.activity.TakeFaceActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CjActivity extends BaseActivity {

    private final static int TAKE_FACE_ACTIVITY_REQUEST_CODE = 12345;//次值自由设置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cj);

    }

    @Override
    protected void onResume() {
        super.onResume();
        findViewById(R.id.activity_main_mStartBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TakeFaceActivity.startActivityForResult(CjActivity.this, TAKE_FACE_ACTIVITY_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_FACE_ACTIVITY_REQUEST_CODE) {
            boolean success = data.getBooleanExtra(ExternalConstant.TAKE_IMAGE_SUCCESS_KEY, false);
            if (success) {
                String imagePath = data.getStringExtra(ExternalConstant.FACE_IMAGE_PATH_KEY);
                showImage(imagePath);
            } else {
                showImage(null);
            }
        }
    }

    private void showImage(String imagePath) {
        if (imagePath == null) {
            ((ImageView) findViewById(R.id.activity_main_mResultImage)).setImageBitmap(null);
            return;
        }
        FileInputStream fis;
        try {
            fis = new FileInputStream(imagePath);
            Bitmap bmp = BitmapFactory.decodeStream(fis);
            fis.close();
            ((ImageView) findViewById(R.id.activity_main_mResultImage)).setImageBitmap(bmp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}